package org.marsdonne.demo4teiid_sb15;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

@SpringBootApplication
public class Demo4teiidSb15Application implements CommandLineRunner {

    @Autowired
    private CustomerRepository customerRepository;

    public static void main(String[] args) {
        SpringApplication.run(Demo4teiidSb15Application.class, args);
    }

    @Override
    @Transactional
    public void run(String... args) throws Exception {
        System.out.println("\n\nFrom All customers entity");
        System.out.println(new Date().getTime());
        AtomicInteger counter = new AtomicInteger();
        customerRepository.findAll().forEach(x -> {
            counter.getAndIncrement();
            System.out.println(x);
        });
        System.out.println(new Date().getTime());
        customerRepository.save(new Customer(counter.get() + 1, "mars", "44-55-6-7"));
        System.out.println(new Date().getTime());
        customerRepository.findAll().forEach(x -> System.out.println(x));
        System.out.println(new Date().getTime());
    }
}
